﻿Public Class oItem
    Public Function GetAllItem(ByVal dataBaseName As String, ByVal companyCode As String, ByVal isAdmin As Boolean) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oItem")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                If (isAdmin) Then
                    ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oItem T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode  ORDER BY ItemID DESC")
                Else
                    ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oItem T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode WHERE T0.CompanyCode = @Param1  ORDER BY ItemID DESC", New Object() {companyCode})
                End If
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetItemsForSearch(ByVal dataBaseName As String, ByVal companyCode As String, ByVal keySearch As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oItem")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oItem T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode WHERE T0.CompanyCode = @Param1 AND (T0.ItemCode LIKE '%' + @Param2 + '%' OR T0.ItemName LIKE '%' + @Param2 + '%' ) ORDER BY ItemID DESC", New Object() {companyCode, keySearch})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetCountItem(ByVal dataBaseName As String, ByVal companyCode As String) As Long
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oItem")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT COUNT(*) AS TotalRow FROM oItem WHERE CompanyCode = @Param1", New Object() {companyCode})
                If ds IsNot Nothing AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                    Return Long.Parse(ds.Tables(0).Rows(0)("TotalRow").ToString())
                End If
            End If
            Return 0
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetItemByCode(ByVal dataBaseName As String, ByVal companyCode As String, ByVal itemCode As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oItem")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oItem  T0 JOIN oCompany  T1 ON T0.CompanyCode = T1.CompanyCode  WHERE ItemCode = @Param1 AND T0.CompanyCode = @Param2", New Object() {itemCode, companyCode})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetItemByID(ByVal dataBaseName As String, ByVal itemID As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oItem")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oItem  T0 JOIN oCompany  T1 ON T0.CompanyCode = T1.CompanyCode  WHERE ItemID = @Param1", New Object() {itemID})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function UpdateJsonItem(
                                            ByVal itemID As String, ByVal itemCode As String, ByVal itemName As String, _
                                            ByVal companyCode As String, ByVal basePrice As Double, ByVal createduserId As String, _
                                            ByVal createdDate As Date, ByVal isActive As Boolean, ByVal allowPromotion As Boolean, _
                                            ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {itemCode, itemName, companyCode, basePrice, createduserId, isActive, allowPromotion}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" INSERT INTO [oItem]")
                    query.Append("  ([ItemCode]")
                    query.Append("  ,[ItemName]")
                    query.Append("  ,[CompanyCode]")
                    query.Append("  ,[BasePrice]")
                    query.Append("  ,[CreatedUserID]")
                    query.Append("  ,[IsActive]")
                    query.Append("  ,[AllowPromotion])")
                    query.Append("VALUES (@Param1,@Param2,@Param3,@Param4,@Param5,@Param6,@Param7)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oItem] SET ")
                    query.Append("  [ItemName] = @Param1")
                    query.Append("  ,[CompanyCode] = @Param2")
                    query.Append("  ,[BasePrice] = @Param3")
                    query.Append("  ,[IsActive] = @Param4")
                    query.Append("  ,[AllowPromotion] = @Param5")
                    query.Append(" WHERE [ItemID] = @Param6")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {itemName, companyCode, basePrice, isActive, allowPromotion, itemID})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function UpdateItem(
                                         ByVal data As DataSet, ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            For Each row As DataRow In data.Tables("oItem").Rows
                Dim errMsg As String = connect.setSQLDB(databaseName)
                If errMsg.Length = 0 Then
                    Dim params = New Object() {row("ItemCode"), row("ItemName"), row("CompanyCode"), row("BasePrice"), row("CreatedUserID"), _
                                              row("IsActive"), row("AllowPromotion")}
                    Dim query As New StringBuilder
                    If isInsert = True Then
                        query.Append(" INSERT INTO [oItem]")
                        query.Append("  ([ItemCode]")
                        query.Append("  ,[ItemName]")
                        query.Append("  ,[CompanyCode]")
                        query.Append("  ,[BasePrice]")
                        query.Append("  ,[CreatedUserID]")
                        query.Append("  ,[IsActive]")
                        query.Append("  ,[AllowPromotion])")
                        query.Append(" VALUES (@Param1,@Param2,@Param3,@Param4,@Param5,@Param6,@Param7)")
                        Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                    Else
                        query.Append("UPDATE  [oItem] SET ")
                        query.Append("  [ItemName] = @Param1")
                        query.Append("  ,[CompanyCode] = @Param2")
                        query.Append("  ,[BasePrice] = @Param3")
                        query.Append("  ,[CreatedUserID] = @Param4")
                        query.Append("  ,[IsActive] = @Param5")
                        query.Append("  ,[AllowPromotion] = @Param6")
                        query.Append("  ,[ItemCode] = @Param7")
                        query.Append(" WHERE [ItemID] = @Param8")
                        connect.Object_Execute_SAP(query.ToString(), New Object() {row("ItemName"), row("CompanyCode"), row("BasePrice"), row("CreatedUserID"), _
                                              row("IsActive"), row("AllowPromotion"), row("ItemCode"), row("ItemID")})
                    End If
                Else
                    Return errMsg
                End If
            Next
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function

End Class
